<?php
namespace controllers;

class BaseControllers{

    protected $twig;
    protected $data = array();

    public function __construct(){
        $loader = new \Twig\Loader\FilesystemLoader(TEMPDIR.'/app/views');
        $this->twig = new \Twig\Environment($loader, [
            // 'cache' => '/path/to/compilation_cache',
        ]);
    }

    protected function assign($var,$value=null){
        if(is_array($var)){
            $this->data = array_merge($this->data,$var);
        }else{
            $this->data[$var] = $value;
        }
       
    }

    protected function display($template){
        $url = getCurUrl();
        $this->assign('uri',$url);
        echo $this->twig->render($template.'.html',$this->data);
    }

    // 发货成功跳转
    protected function topsuccess($url,$mess){
        echo "<script>";
        echo "alert('{$mess}');";
        if(!empty($url)){
            echo "top.location.href='{$url}';";
        }
        echo "</script>";
    }
    // 成功跳转
    protected function success($url,$mess){
        echo "<script>";
        echo "alert('{$mess}');";
        if(!empty($url)){
            echo "location.href='{$url}';";
        }
        echo "</script>";
    }
    
    // 失败跳转
    protected function error($url,$mess){
        echo "<script>";
        echo "alert('ERROR:{$mess}');";
        if(!empty($url)){
            echo "location.href='{$url}';";
        }
        echo "</script>";
    }
}